<?php

namespace pmvc\template\twig;

use pmvc\ioc\FactoryObject;
use pmvc\util\ClassUtil;

/**
 * A {@link FactoryObject} for creating and
 * configuring a {@link Twig_Environment}.
 */
class Twig_EnvironmentFactoryObject
	implements FactoryObject {

	public $registerAutoLoader		= true;
	public $autoLoaderPath 			= 'Twig/Autoloader.php';
    public $templatePaths			= Array();
    public $debug					= null;
    public $charset					= null;
    public $baseTemplate_class		= null;
    public $cacheDir				= null;
    public $autoReload				= null;
    public $strict					= null;
    public $autoEscape				= null;
    public $lexer					= null;
    public $parser					= null;
    public $compiler				= null;
    public $extensions				= Array();
    public $tokenParsers			= Array();
    public $nodeVisitors			= Array();
    public $filters					= Array();
    public $tests					= Array();
    public $functions				= Array();
    public $globals					= Array();
    
    /**
     * {@inheritdoc}
     */
	public function createObject() {

		// register the auto loader
		if ($this->registerAutoLoader===true) {
			if (!ClassUtil::requireFile($this->autoLoaderPath, TRUE)) {
				require_once($this->autoLoaderPath);
			}
		}

		// do the autoloading
		if (!class_exists('Twig_Autoloader', false)) {
			throw new Exception("Twig_Autoloader class not loaded");
		}
		\Twig_Autoloader::register();

		// create the loader
		$loader = new \Twig_Loader_Filesystem($this->templatePaths);

		// create enviornment options array
		$envOptions = Array();
		$setEnvFun = function($name, $value)use($envOptions) {
			if (isset($value)) {
				$envOptions["name"] = $value;
			}
		};
		$setEnvFun("debug",					$this->debug);
		$setEnvFun("charset",				$this->charset);
		$setEnvFun("base_template_class",	$this->base_template_class);
		$setEnvFun("cache",					$this->cacheDir);
		$setEnvFun("auto_reload",			$this->autoReload);
		$setEnvFun("struct_variables",		$this->strict);
		$setEnvFun("auto_escape",			$this->autoEscape);

		// create the environment
		$env = new \Twig_Environment($loader, $envOptions);

		// function for instantiating objects if needed
		$asObj = function($val) {
			return is_string($val) ? new $val() : $val;
		};

		// set lexer
		if (isset($this->lexer)) {
			$env->setLexer($this->lexer);
		}

		// set parser
		if (isset($this->parser)) {
			$env->setParser($this->parser);
		}

		// set compiler
		if (isset($this->compiler)) {
			$env->setCompiler($this->compiler);
		}

		// add token parsers
		foreach ($this->tokenParsers as $tp) {
			$env->addTokenParser($asObj($tp));
		}

		// add node visitors
		foreach ($this->nodeVisitors as $nv) {
			$env->addNodeVisitor($asObj($nv));
		}

		// add extensions
		foreach ($this->extensions as $ext) {
			$env->addExtension($asObj($ext));
		}

		// add filters
		foreach ($this->filters as $pair) {
			$name = $pair[0];
			$filter = $pair[1];
			$env->addFilter($name, $asObj($filter));
		}

		// add tests
		foreach ($this->tests as $pair) {
			$name = $pair[0];
			$test = $pair[1];
			$env->addTest($name, $asObj($test));
		}

		// add functions
		foreach ($this->functions as $pair) {
			$name = $pair[0];
			$func = $pair[1];
			$env->addFunction($name, $asObj($func));
		}

		// add globals
		foreach ($this->globals as $pair) {
			$name = $pair[0];
			$global = $pair[1];
			$env->addGlobal($name, $global);
		}

		// return it
		return $env;
	}

}

?>